package com.team.arms.xitong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.arms.common.Result;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.util.ConvertUtil;
import com.team.arms.utils.PageUtil;
import com.team.arms.xitong.entity.LogManage;
import com.team.arms.xitong.entity.SystemAccess;
import com.team.arms.xitong.entity.dto.LogDto;
import com.team.arms.xitong.entity.vo.LogVo;
import com.team.arms.xitong.mapper.LogManageMapper;
import com.team.arms.xitong.mapper.SystemAccessMapper;
import com.team.arms.xitong.service.ILogManageService;
import com.team.arms.xitong.service.ISystemAccessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Huazige
 * @since 2022-06-24
 */
@RestController
@RequestMapping("/log")
@CrossOrigin
@Api(tags = "系统管理-日志管理")
public class LogManageController {
    @Autowired
    LogManageMapper logManageMapper;
    @Autowired
    ILogManageService iLogManageService;
    @Autowired
    SystemAccessMapper systemAccessMapper;
    @Autowired
    ISystemAccessService iSystemAccessService;
    @Autowired
    ConvertUtil convertUtil;

    @ApiOperation("分页显示所有日志信息")
    @GetMapping(value = "/list/{pageNum}/{pageSize}")
    public Result selectMsg(
            @ApiParam("当前页数")       @PathVariable("pageNum")Integer pageNum,
            @ApiParam("每页的数据量")       @PathVariable("pageSize")Integer pageSize

    ){
        List<LogVo> dicTypes = logManageMapper.select();

        Page page = PageUtil.list2Page(dicTypes, pageNum, pageSize);
        return page !=null ? Result.success("分页成功").data(page): Result.error("分页失败");
    }
    @ApiOperation("修改日志信息")
    @PutMapping("/update")
    public AjaxResult updateSchedule(@RequestBody LogVo logVo){

        boolean b =   logManageMapper.update(logVo);//前新后旧
        return b ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }
    @ApiOperation("单个+批量删除日志信息")
    @DeleteMapping("/deletelist")
    public Result deleteInformation(
            @ApiParam("访问表编号") @RequestParam("accessId")List<String> deletelist ) {
        boolean b = iSystemAccessService.removeByIds(deletelist);
//        for ( String accessId:deletelist
//        ) {
////            QueryWrapper<BingyiInfo> wrapper2 = new QueryWrapper();
////            wrapper2.eq("bingyi_info_id",id);
////            bingyiInfoMapper.delete(wrapper2);
//            QueryWrapper<SystemAccess> wrapper2 = new QueryWrapper();
//            wrapper2.eq("access_id",accessId);
//            systemAccessMapper.delete(wrapper2);
//        }
//
//
//
//        boolean b = iLogManageService.removeByIds(deletelist);
        return b ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }
    @ApiOperation("新增日志信息")
    @PostMapping("/inster")
    @Transactional
    public Result install(@RequestBody LogDto logDto){
        String logType = logDto.getLogType();
        QueryWrapper<LogManage> wrapper2 = new QueryWrapper();
        wrapper2.eq("log_type",logType);
        LogManage logManage = logManageMapper.selectOne(wrapper2);
        if (logManage!=null){
            SystemAccess systemAccess = new SystemAccess();
            systemAccess.setLogId(logManage.getLogId());
            systemAccess.setUsername(logDto.getUsername());
            systemAccess.setLoginPlace(logDto.getLoginPlace());
            systemAccessMapper.insert(systemAccess);
            return Result.success("添加成功").data(systemAccess);
        }
        LogManage logManage1 = new LogManage();
        logManage1.setLogType(logDto.getLogType());
        logManageMapper.insert(logManage1);
        String logId = logManage1.getLogId();
        SystemAccess systemAccess = new SystemAccess();
        systemAccess.setLogId(logId);
        systemAccess.setUsername(logDto.getUsername());
        systemAccess.setLoginPlace(logDto.getLoginPlace());
        systemAccessMapper.insert(systemAccess);
        return Result.success("添加成功").data(systemAccess);

    }
}

